var geocoder; 

//Get the latitude and the longitude; 
function successFunction(position) { 
    var lat = position.coords.latitude; 
    var lng = position.coords.longitude; 
    codeLatLng(lat, lng); 
} 
 
function errorFunction(){ 
    alert("Geocoder failed"); 
} 
 
function initialize() { 
    geocoder = new google.maps.Geocoder(); 			 
} 
 
function codeLatLng(lat, lng) { 
	var success = false;
	
	geocoder = new google.maps.Geocoder(); 
    var latlng = new google.maps.LatLng(lat, lng); 
    geocoder.geocode({'latLng': latlng}, function(results, status) { 
      if (status == google.maps.GeocoderStatus.OK) { 
      console.log(results); 
        if (results[1]) { 
         //formatted address 
         //alert(results[0].formatted_address)
         var appendString = '<div align="center"><p><b>GeoLocation has located you at: <b></p></div>' + 
         			'<div align="center"><img src="http://maps.googleapis.com/maps/api/staticmap?center=' + lat + ',' + lng + '&zoom=12&size=300x200&markers=color:red%7Ccolor:red%7Clabel:Mesh%7C' + lat + ',' + lng + '&sensor=false"/></div>' + 
	      						'<div align="center"><p>' + results[0].formatted_address + '</p></div>'; 
         
         
         
         success = true;
         
        //find zipcode 
        
        for (var i=0; i<results[0].address_components.length; i++) { 
            for (var b=0;b<results[0].address_components[i].types.length;b++) { 
 
            //there are different types that might hold a city admin_area_lvl_1 usually does in come cases looking for sublocality type will be more appropriate 
                if (results[0].address_components[i].types[b] == "postal_code") { 
                    //this is the object you are looking for 
                    var addZip= results[0].address_components[i]; 
                    break; 
                } 
            } 
        } 
        //city data 
        //alert(city.short_name + " " + city.long_name) 

         $('.location').append(appendString);
         var textbox = document.getElementById('txtzip');
         if (typeof addZip != 'undefined' || addZip != null) {
        	 if (textbox !== null) {
         		textbox.value = addZip.long_name;  
        	 }
         }
        } else { 
          alert("No results found"); 
        } 
      } else { 
        alert("Geocoder failed due to: " + status); 
      } 
    }); 
    
    return success;
} 

function go()
{
	var zip =  document.getElementById("txtzip");
	var zipValue = zip.value;
	sessionStorage.setItem('zip', zipValue);
}